﻿using Microsoft.AspNetCore.Mvc;
using Mojito.Model;
using Mojito.Services;
using System.Linq;
using System.Threading.Tasks;

namespace Mojito.Controllers
{
    /// <summary>
    ///  请求目录
    /// </summary>
    [Route("api/dir")]
    public class DirectoriesController : ControllerBase
    {
        public DirectoriesService _DirectoriesService { get; set; }

        /// <summary>
        ///  获取工作区根目录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetWorkspace()
        {
            var output = await _DirectoriesService.GetMyWorkspace(CurrentUser.Id);
            return Success(data: output);
        }

        /// <summary>
        ///  添加工作区根目录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> SaveWorkspace([FromBody] DirectoryInput input)
        {
            var output = await _DirectoriesService.SaveWorkspace(CurrentUser.Id, input);
            return Success(data: output);
        }

        /// <summary>
        ///  删除工作区
        /// </summary>
        /// <returns></returns>
        [HttpDelete("{Id}")]
        public async Task<IActionResult> DestroyWorkspace(string Id)
        {
            await _DirectoriesService.DestroyWorkspace(Id);
            return Success("删除成功");
        }

        /// <summary>
        ///  获取子目录树
        /// </summary>
        /// <param name="id">工作区目录</param>
        /// <returns></returns>
        [HttpGet("items/{id}")]
        public async Task<IActionResult> GetDirectories(string id)
        {
            var output = await _DirectoriesService.GetDirectories(id);
            return Success(data: output);
        }

        /// <summary>
        ///  添加子目录树
        /// </summary>
        /// <param name="id">工作区目录</param>
        /// <returns></returns>
        [HttpPost("items")]
        public async Task<IActionResult> SaveDirectory([FromBody] DirectoriesOutput input)
        {
            var output = await _DirectoriesService.SaveDirectory(input);
            return Success(data: output);
        }

        /// <summary>
        ///  删除子目录树
        /// </summary>
        /// <param name="id">工作区目录</param>
        /// <returns></returns>
        [HttpDelete("items/{Id}")]
        public async Task<IActionResult> DestroyDirectory(string Id)
        {
            var output = await _DirectoriesService.DestroyDirectory(Id);
            return Success(data: output);
        }
    }
}
